/*
 * libmanyuc - Turn leds on in C example
 * Copyright (C) 2012 - Margarita Manterola Rivero
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include "libmanyuc.h"

/* This example counts the number of interrupts received on
 * pin 10, showing the number in binary using the leds. */

// Conting variable
TimeCounter_t counter;

// Serial port file descriptor
FILE *f;

Pin_t pin;

void output(void) {
    Pin_Toggle(pin);
    fprintf(f, "Microseconds passed: %d\r\n", TimeCounter_Amount_us(counter));
    fprintf(f, "Miliseconds passed: %d\r\n", TimeCounter_Amount_ms(counter));
    fprintf(f, "Seconds passed: %f\r\n", TimeCounter_Amount(counter));
}

int main(void) {

    // Initialize serial port
    Serial_t port = Serial_Init(0, 9600);
    f = Serial_Get_File(port);
    fprintf(f, "Welcome to the measurement program \r\n");

    // The button is in P16
    Pin_t in = Pin_Init(P16, 2, Input, PullUp);

    pin = Pin_Init(LED1, 1, Output);

    // Start counting
    TimeCounter_t counter = TimeCounter_Init();

    // Associate the interrupt
    Pin_Int_Attach(in, output, IOIntFall);

    while (1) {
        // Do nothing
        Delay(1);
    }
}
// vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
